package com.manxi.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manxi.constant.MessageConstant;
import com.manxi.constant.StatusConstant;
import com.manxi.context.BaseContext;
import com.manxi.dto.SetmealDTO;
import com.manxi.dto.SetmealPageQueryDTO;
import com.manxi.entity.Setmeal;
import com.manxi.entity.SetmealDish;
import com.manxi.exception.DeletionNotAllowedException;
import com.manxi.mapper.SetmealDishMapper;
import com.manxi.mapper.SetmealMapper;
import com.manxi.result.PageResult;
import com.manxi.service.SetmealService;
import com.manxi.vo.DishItemVO;
import com.manxi.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult PageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.PageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {

        //获取菜品信息
        Setmeal setmeal = setmealMapper.getById(id);

        //根据套餐id查询套餐菜品表信息
        List<SetmealDish> sdList = setmealDishMapper.getSetmealDishBySetmealId(id);

            SetmealVO setmealVO = new SetmealVO();

            BeanUtils.copyProperties(setmeal,setmealVO);

            setmealVO.setSetmealDishes(sdList);

            return setmealVO;

    }

    /**
     * setmealDTO
     *
     * @param setmealDTO
     */
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setCount(0);

        //新增套餐
        setmealMapper.insert(setmeal);

        //获取新增套餐的id
        Long setmealId = setmeal.getId();

        //获取套餐菜品列表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && setmealDishes.size() > 0) {
            //遍历套餐菜品表，设置套餐菜品表的套餐id
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
        }

        //保存套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.update(setmeal);

        //获取套餐id
        Long setmealId = setmeal.getId();

        //删除套餐和菜品相关信息
        setmealDishMapper.detele(setmealId);

        //重新添加新的套餐菜品相关表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null &&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });

            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 启售禁售套餐
     * @param status
     */
    public void startOrStop(Integer status,Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除或根据id删除套餐
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {

        //启售的套餐不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        for (Long id : ids) {
            //删除套餐
            setmealMapper.deleteBatch(id);

            //删除套餐和菜品关联表
            setmealDishMapper.detele(id);

        }
    }

    /**
     * 根据分类id查询套餐
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {

       List<Setmeal> list = setmealMapper.list(setmeal);

       return list;
    }

    /**
     * 根据套餐id查询包含的菜品列表
     * @param setmealId
     * @return
     */
    public List<DishItemVO> getDishItemBySetmealId(Long setmealId) {
        List<DishItemVO> lsit = setmealMapper.getDishItemBySetmealId(setmealId);
        return lsit;
    }
}
